/*
 * TowerBlueTwo.cs
 *
 * Created on 27 December 2007, 15:14
 *
 * Copyright (C) 2007-2008  Hylke van der Schaaf
 * Copyright (C) 2010       Mathijs Miermans
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, in version 3 of the License.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 */

using System.Windows.Media;
using System.Windows.Shapes;
using System.Windows.Controls;

using HexTD.hex;
using HexTD.sprite;
using HexTD.util;

namespace HexTD.tower
{
	public class TowerBlueTwo : TowerBase
	{

		public static int price = 50000;
		public static int damage = 0;
		public static float range = 5f;

		private int fireAt = -1;
		private float enemyX = 0;
		private float enemyY = 0;
		private COLOR damageColor = COLOR.BLUE;
		private int coolDown = 0;
		private int coolDownMax = 1;

		private Color transColor;
		private string[] newModes = { "Fastest" };

		private EnemySprite[] currentTargets = new EnemySprite[0];

		/** Creates a new instance of TowerBlueTwo */
		public TowerBlueTwo(Context context, Hex hex)
			: base(TowerFactory.type.blueTwo, price, damage, range)
		{
			this.lineColor = Color.FromArgb(0xFF, 75, 75, 255);
			this.transColor = Color.FromArgb(75, this.lineColor.R, this.lineColor.G, this.lineColor.B);
			this.modes = this.newModes;
			this.doInit(context, hex);
		}

		public override string getStatusString()
		{
			string retString = "";
			retString += "<h1>Blue Tower 2</h1>" +
					"<p>" +
					"<b>Level:</b> " + (1 + this.level) + "<br>\n" +
					"<b>Slowdown:</b> " + (100f - 100f / (2 + this.level)).ToString() + "%<br>\n" +
					"<b>Range:</b> " + this.rangeText + "<br>" +
					"</p>\n";
			return retString;
		}

		public override string getInfoString()
		{
			string retString = "";
			retString += "<h1>Blue Tower 2</h1>" +
					"<p>" +
					"Tower with four energy beams that overheat the targets reactor, reducing it's power output and thus it's speed. " +
					"More hits make the effect last longer and upgrading the tower makes the effect stronger." +
					"<br>It's effect is strong against blue enemies but very weak against yellow enemies." +
					"</p>" +
					"<p>" +
					"<b>Price:</b> " + price + "<br>" +
					"<b>Damage:</b> none, slowdown instead<br>" +
					"<b>Range:</b> " + rangeCurrent + "<br>" +
					"<b>Refire rate:</b> 0.25/s" +
					"</p>";
			return retString;
		}

		public override void doTick(int gameTime)
		{
#if PORT
			if (this.coolDown > 0)
			{
				this.coolDown--;
			}
			else
			{
				this.currentTargets = this.findEnemiesFastest(this.xFloat, this.yFloat, this.rangeCurrent, this.currentTargets, 4, false);
				if (this.currentTargets.length > 0)
				{
					EnemySprite enemy;
					for (int i = 0; i < this.currentTargets.length; i++)
					{
						enemy = this.currentTargets[i];
						enemy.doDamage(1f / (2f + this.level), sprite.DamageType.speed, this.damageColor);
					}

					//System.Diagnostics.Debug.WriteLine("TR! fire at tick: "+gameTime);
					this.coolDown = this.coolDownMax;
				}
			}
#endif
		}

		protected override void generateShapes()
		{
#if PORT

			int shapeCount = 3;
			int i;
			float[,] points = Geometry.createPolyCoords(this.paintScale);
			float[,] points2 = Geometry.createPolyCoords(this.paintScale * 0.9f);

			this.shapes = new Shape[shapeCount];
			i = 0;
			GeneralPath poly = new GeneralPath(GeneralPath.WIND_EVEN_ODD);
			poly.append(new CubicCurve2D.Float(points[0, 0], points[0, 1], points2[5, 0], points2[5, 1], points2[3, 0], points2[3, 1], points[2, 0], points[2, 1]), true);
			poly.append(new CubicCurve2D.Float(points[2, 0], points[2, 1], points2[1, 0], points2[1, 1], points2[5, 0], points2[5, 1], points[4, 0], points[4, 1]), true);
			poly.append(new CubicCurve2D.Float(points[4, 0], points[4, 1], points2[3, 0], points2[3, 1], points2[1, 0], points2[1, 1], points[0, 0], points[0, 1]), true);
			poly.append(new CubicCurve2D.Float(points[1, 0], points[1, 1], points2[0, 0], points2[0, 1], points2[4, 0], points2[4, 1], points[3, 0], points[3, 1]), false);
			poly.append(new CubicCurve2D.Float(points[3, 0], points[3, 1], points2[2, 0], points2[2, 1], points2[0, 0], points2[0, 1], points[5, 0], points[5, 1]), true);
			poly.append(new CubicCurve2D.Float(points[5, 0], points[5, 1], points2[4, 0], points2[4, 1], points2[2, 0], points2[2, 1], points[1, 0], points[1, 1]), true);
			this.shapes[i++] = new Area(poly);
			this.shapes[i++] = this.shapes[0];
			this.shapes[i++] = new Ellipse.Float(-1.0f * this.context.strokeScale, -1.0f * this.context.strokeScale, 2.0f * this.context.strokeScale, 2.0f * this.context.strokeScale);

			i = 0;
			this.shapeAction = new int[shapeCount];
			this.shapeAction[i++] = 2;
			this.shapeAction[i++] = 1;
			this.shapeAction[i++] = 1;

			i = 0;
			this.shapeColor = new Color[shapeCount];
			this.shapeColor[i++] = this.transColor;
			this.shapeColor[i++] = this.lineColor;
			this.shapeColor[i++] = this.lineColor;

			i = 0;
			this.strokes = new Brush[shapeCount];
			this.strokes[i++] = new Brush(2.0f * this.context.strokeScale, Brush.CAP_ROUND, Brush.JOIN_BEVEL);
			this.strokes[i++] = this.strokes[0];
			this.strokes[i++] = this.strokes[0];
#endif
		}

		public override void paintEffects(Canvas g2, int gameTime)
		{
#if PORT
			Brush defaultStroke = g2.getStroke();

			if (this.currentTargets.Length > 0 && this.coolDown > this.coolDownMax - 2)
			{
				g2.setColor(this.lineColor);
				for (int i = 0; i < this.currentTargets.Length; i++)
				{
					g2.draw(new LineSegment.Float(this.xPaint, this.yPaint, currentTargets[i].getPaintX(), currentTargets[i].getPaintY()));
				}
			}

			g2.setStroke(defaultStroke);
#endif
		}

	}
}